import moment from 'moment';

import {Message, Notification, Loading} from 'element-ui'
import cookies from './util-cookies'
import db from './util-db'
import log from './util-log'

export default {

  cookies,
  db,
  log,
  formatDts(val, fmt) {
    if (val) {
      val = moment(val).format(fmt || 'YYYY-MM-DD');
    } else {
      val = '---';
    }
    return val;
  },
  showLoading() {
    Loading.service({
      lock: true,
      text: '加载中...',
      customClass: 'cust-loading',
      spinner: 'fa fa-spinner',
      background: 'rgba(0, 0, 0, 0.6)'
    });
  },
  hideLoading() {
    Loading.service().close();
  },
  dateResize(date) {//date转时间戳
    if (date) {
      var newdate = new Date(date);
      var time = newdate.getTime();
      return time
    }
  },
  showTips(msg, type, title) {
    Notification({title: title || '操作提示', message: msg, type: type == false ? 'error' : 'success'});
    // 显示提示
    // Message({
    //   message: msg,
    //   type: type == false ? 'error' : (!type ? 'success' : type),
    //   showClose: true,
    //   duration: 15 * 1000
    // })
  },
  /**
   * @description 更新标题
   * @param {String} title 标题
   */
  title(titleText) {
    const processTitle = process.env.VUE_APP_TITLE || '监控管理平台';
    window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
  },

  /**
   * @description 打开新页面
   * @param {String} url 地址
   */
  open(url) {
    var a = document.createElement('a')
    a.setAttribute('href', url)
    a.setAttribute('target', '_blank')
    a.setAttribute('id', 'd2admin-link-temp')
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(document.getElementById('d2admin-link-temp'))
  },


  generatorDeptTree(listData, readonly) {
    var getJsonTree = function (dataList, parentId) {
      var itemArr = [];
      for (var i = 0; i < dataList.length; i++) {
        var node = dataList[i];
        if (node.pid == parentId) {
          node.children = getJsonTree(dataList, node.id);
          node.label = node.label || node.name;
          node.disabled = readonly;
          itemArr.push(node);
        }
      }
      itemArr.sort((a, b) => {
        return a.name.localeCompare(b.name);
      })
      return itemArr;
    };

    //获取根节点 start
    let rootPaths = {};
    listData.forEach(item => {
      item.pid = item.pid == null ? -1 : item.pid;
      if (!rootPaths[item.pid]) {
        rootPaths[item.pid] = item.pid;
      }
    });
    listData.forEach(item => {
      delete rootPaths[item.id];
    });
    //获取根节点 end
    var treeList = [];
    for (let id in rootPaths) {
      treeList = treeList.concat([], getJsonTree(listData, id));
    }
    return {treeList: treeList};
  },

  pushRouterStatus(list, currRouter, targetRouter) {
    for (let i = 0; i < list.length; i++) {
      let item = list[i];
      if (item.name == currRouter.name) {
        let newRouter = targetRouter[0].children[0];
        list.splice(i + 1, 0, newRouter);
        return;
      }

      if (item.children) {
        this.pushRouterStatus(item.children, currRouter, targetRouter)
      }
    }
  },
  getPageheight(offset) {
    return this._getHeight(offset);
  },
  getPageSize(noHeader) {
    let contentHeight = this._getHeight();
    let page = 20;
    if (contentHeight) {
      contentHeight = contentHeight - (noHeader ? 0 : 45);
      page = parseInt(contentHeight / 34);
    }
    return page;
  },
  _getHeight(offset) {
    let dom = document.querySelector('.d2-layout-header-aside-group');
    if (!dom) {
      dom = document.querySelector('body');
    }

    offset = offset || {};
    let fullHeight = dom.clientHeight;
    let contentHeight = fullHeight
      - 60//顶部菜单
      - 45//多页tab
      - 20//上下间距
      - (offset.noHeader ? 0 : 40)//列头
      - (offset.noPage ? 0 : 38)//分页
    ;
    return contentHeight;
  }
}
